package org.evolution.util.db;

import java.io.Serializable;
import java.util.UUID;

import org.apache.activemq.memory.LRUMap;
import org.apache.log4j.Logger;

public class PageQueryUtil {

    private static final Logger logger = Logger.getLogger(PageQueryUtil.class);

    private static LRUMap countQuerys = new LRUMap(500);

    private static Serializable nextId() {
        Serializable id = UUID.randomUUID().toString().replace("-", "");
        logger.debug("生成分页查询id：" + id);
        return id;
    }

    public static synchronized Serializable add(Countor coutor) {
        Serializable id = nextId();
        countQuerys.put(id, coutor);
        return id;
    }

    public static synchronized Countor get(Serializable id) {
        return (Countor) countQuerys.get(id);
    }
    
    @SuppressWarnings("unchecked")
    public static PageData getCountQueryResult(Serializable id){
        Countor c = get(id);
        if (c == null) {
            return new PageData();
        }
        c.count();
        return c.getPageData();
    }
    
    public static void cancelCountQuery(Serializable id){
        Countor c = get(id);
        if (c != null) {
            c.cancel();
        }
    }

}